package com.zxcl.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zxcl.domain.SysRole;
import com.zxcl.dto.PageDTO;
import com.zxcl.dto.SysRoleQueryDTO;
import com.zxcl.model.Result;
import com.zxcl.service.SysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 描述: [角色管理接口]
 * 作者: zxcl
 */
@RestController
@Api(tags = "角色管理接口")
@RequestMapping("sys/role")
public class SysRoleController {

    @Autowired
    private SysRoleService sysRoleService;


    /**
     * 查询所有角色
     * 如果是超级店铺的员工，可以查询出所有角色，如果不是，只查非管理员的角色
     */
    @GetMapping("/page")
    @ApiOperation("分页查询所有角色")
    @PreAuthorize(value = "hasAuthority('sys:role:list')")
    public Result<Page<SysRole>> loadSysRoleList(PageDTO pageDTO, SysRoleQueryDTO sysRoleQueryDTO){
        Page<SysRole> sysRolePage=sysRoleService.loadSysRolePage(pageDTO,sysRoleQueryDTO);
        return Result.success(sysRolePage);
    }


    @GetMapping("/table")
    @ApiOperation("查询所有的角色")
    public Result<?> getMenuList(){
        List<SysRole> list=sysRoleService.loadSysRoleList();
        return Result.success(list);
    }

    @GetMapping("info/{userId}")
    @ApiOperation("获取当前角色信息")
    public Result<SysRole> loadMenu(@PathVariable Long userId){
        SysRole sysRole = sysRoleService.getById(userId);
        return Result.success(sysRole);
    }

    @DeleteMapping()
    @ApiOperation("删除当前角色的信息")
    public Result<String> removeUser(@RequestBody List<Long> ids) {
        boolean row = sysRoleService.removeByIds(ids);
        return Result.handle(row);
    }

    @PostMapping
    @ApiOperation("添加角色的信息")
    public Result<String> addSysRole(@RequestBody SysRole sysRole) {
        sysRole.setCreateTime(new Date());
        boolean flag = sysRoleService.save(sysRole);
        return Result.handle(flag);
    }

    @PutMapping
    @ApiOperation("修改角色的信息")
    public Result<String> updateSysRole(@RequestBody SysRole sysRole) {
        sysRole.setCreateTime(new Date());
        boolean flag = sysRoleService.updateById(sysRole);
        return Result.handle(flag);
    }

}
